package com.aaa.three.controller;

import com.aaa.three.entity.Registered;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.RecordsService;
import com.aaa.three.service.ResultService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.*;
import java.util.HashMap;

/**
 * @PACKAGE_NAME: com.aaa.three.controller
 * @NAME: RecordsDownController
 * @USER: Tprairie
 * @DATE: 2022/2/25
 * @PROJECT_NAME: three
 */
@Controller
@RequestMapping("/resour")
public class RecordsDownController {
    @Autowired
    private RecordsService recordsServiceImpl;
    @Autowired
    private ResultService resultServiceImpl;
    @RequestMapping("/down")
    public ResponseEntity<byte[]> recordsDown(Registered registered) throws Exception {
        AjaxResult detailedRecords = recordsServiceImpl.getDetailedRecords(registered);
        Registered data = (Registered) detailedRecords.getData();
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        String s = Thread.currentThread().getContextClassLoader().getResource("").toString();
        s="/"+s;
        s=s.replace("/", File.separator);
        s=s.replace("file:","");
        s=s.replace("classes"+File.separator,"");
        s=s.substring(1);
        configuration.setDirectoryForTemplateLoading(new File(s));
        Template template = configuration.getTemplate("病历.ftl");
        HashMap<String, String> map = new HashMap<>();
        map.put("name",data.getName());
        map.put("sex",data.getSex());
        map.put("idcard",data.getIdcard());
        map.put("telephone",data.getTelephone());
        map.put("registrationdate",data.getRegistrationdate());
        map.put("oname",data.getOname());
        map.put("content",data.getContent());
        map.put("dealwith",data.getDealwith());
        map.put("aname",data.getAname());
        String outFilePath=data.getName()+"病历.doc";
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
        template.process(map,out);
        if(out!=null){
            out.close();

        }
        HttpHeaders headers = new HttpHeaders();
        String fileName=outFilePath;
        String downLoadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        headers.setContentDispositionFormData("attachment",downLoadFielName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(docFile),headers, HttpStatus.CREATED);
    }
    @RequestMapping("/indivualDown")
    public ResponseEntity<byte[]> indivualDown(Integer id) throws Exception {
        Registered data=resultServiceImpl.getrecords(id);
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        String s = Thread.currentThread().getContextClassLoader().getResource("").toString();
        s="/"+s;
        s=s.replace("/", File.separator);
        s=s.replace("file:","");
        s=s.replace("classes"+File.separator,"");
        s=s.substring(1);
        configuration.setDirectoryForTemplateLoading(new File(s));
        Template template = configuration.getTemplate("病历.ftl");
        HashMap<String, String> map = new HashMap<>();
        map.put("name",data.getName());
        map.put("sex",data.getSex());
        map.put("idcard",data.getIdcard());
        map.put("telephone",data.getTelephone());
        map.put("registrationdate",data.getRegistrationdate());
        map.put("oname",data.getOname());
        map.put("content",data.getContent());
        map.put("dealwith",data.getDealwith());
        map.put("aname",data.getAname());
        String outFilePath=data.getName()+"病历.doc";
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
        template.process(map,out);
        if(out!=null){
            out.close();

        }
        HttpHeaders headers = new HttpHeaders();
        String fileName=outFilePath;
        String downLoadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        headers.setContentDispositionFormData("attachment",downLoadFielName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(docFile),headers, HttpStatus.CREATED);
    }
}
